package com.csweimei.rentmanagementtreasure.pop;

import android.app.Activity;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.utils.permission.MyCustomPopupWindow;
import com.csweimei.rentmanagementtreasure.weight.selAddress.MyAddressPickerView;
import com.lxj.xpopup.core.BottomPopupView;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopAddQuarters extends BottomPopupView {

    private TextView cancel,sure,tvCity;
    private EditText name,address;
    private PopSelClick mPopSelClick;
    private Activity mActivity;


    public PopAddQuarters(@NonNull Context context,PopSelClick popSelClick) {
        super(context);
        mPopSelClick = popSelClick;
        mActivity = (Activity) context;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_add_quarter;
    }

    @Override
    protected void onCreate() {
        super.onCreate();

        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);

        tvCity = findViewById(R.id.tc_city);
        name = findViewById(R.id.edt_name);
        address = findViewById(R.id.edt_d_address);

        sure.setOnClickListener(v -> {
            if (ValidateUtils.isEmptyString(name.getText().toString())){
                ToastUtils.showShort("请输入小区名称");
                return;
            }
            if (ValidateUtils.isEmptyString(tvCity.getText().toString())){
                ToastUtils.showShort("请选择小区所在城市");
                return;
            }

            if (ValidateUtils.isEmptyString(address.getText().toString())){
                ToastUtils.showShort("请选择小区所在详细地址");
                return;
            }

            mPopSelClick.sel(new PopBean(name.getText().toString()+"&&"+tvCity.getText().toString()+"&&"+address.getText().toString()));
            dismiss();

        });

        cancel.setOnClickListener(v -> {
            dismiss();
        });

        tvCity.setOnClickListener(v -> {
            View rootView = View.inflate(mActivity, R.layout.dialog_address_my, null);
            MyAddressPickerView addressView = rootView.findViewById(R.id.apvAddress);

            MyCustomPopupWindow mPopupWindow = MyCustomPopupWindow.builder()
                    .contentView(rootView)
                    .parentView(getRootView())
                    .customListener((contentView) -> {
                    })
                    .activity(mActivity)
                    .isWrapHeight(true)
                    .gravity(Gravity.BOTTOM)
                    .build();
            mPopupWindow.show(0.5f);
            addressView.setOnAddressPickerSure((address, province, provinceCode, city, cityCode, area, areaCode) -> {
                if (city.equals("市辖区") || city.equals("县")) {
                    tvCity.setText(province);
                } else {
                    tvCity.setText(city);
                }
                mPopupWindow.dismiss();
            });

            addressView.setOnAddressPickerCancelListener(() -> {
                tvCity.setText("请选择");
                mPopupWindow.dismiss();
            });
        });

    }
}
